// pages/category/category.js
Page({
  data: {
    leftData:[],
    rightData:[],
    isactive:0,
    scrollTop:0
  },
  cateList:[],
  rightList:[],
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
      mask:true
    });
    // 判断本地是否存在缓存数据
    const storageData = wx.getStorageSync('data')
    if (!storageData) {
      this.getCategory()
      .then((data)=>{
      this.cateList = data;
      // 储存到本地缓存
      wx.setStorageSync('data',data);
      console.log("请求数据");
      })
    }else{
      this.cateList = storageData;
      console.log("本地数据")
    }
    this.changedata();
    this.setData({rightData:this.rightList[this.data.isactive]});
    wx.hideLoading();
  },
  //获取分类数据
  getCategory(){
    return new Promise((resovel,reject)=>{
      wx.request({
        url: 'https://api-hmugo-web.itheima.net/api/public/v1/categories',
        success:(res)=>{
          const data = res.data.message
          resovel(data)
        }
      })
    })
  },
  // 处理获取数据
  changedata(){
    const leftList = this.cateList.map(item=>item.cat_name)
    this.setData({leftData:leftList});
    const rightList = this.cateList.map(item=>item.children)
    this.rightList = rightList;
  },
  titleClick(data){
    const {index} = data.currentTarget.dataset;
    // 左侧点击属性样式
    this.setData({isactive:index});
    // 更改右侧品牌信息
    this.setData({rightData:this.rightList[this.data.isactive]});
    // 点击后右侧滚动置顶
    this.setData({scrollTop:0})
  }
})